import { makeAutoObservable, observable } from 'mobx';
import { t } from '@lingui/macro';
import * as api from './api';

export default class DriverGroupStore {
  _args = {
    search: {
    },
    pg: {
      page: 0,
      sort: ['crDate;DESC'],
      size: 10,
    },
  };
  total = 0;
  refund = [];
  loading = false;


  _MESSAGE_TEXT = {
    1: t`不能重复设置价格区间，请重新设置！`,
    2: t`结束价格不能小于开始价格，请重新设置！`,
  };

  constructor(rootStore) {
    this.rootStore = rootStore;
    makeAutoObservable(this, { rootStore: false });
  }

  /**
   * 使用设置的请求参数和分页配置请求列表数据
   * 分页参数为空时保持当前分页配置
   * @param {object} pgInfo 分页参数
   */
  * query(pgInfo) {
    _.mergeWith(this._args.pg, pgInfo, (o, s) => (_.isArray(o) ? s : undefined)); // 数组直接赋值，不合并
    const { search, pg } = toJS(this._args);
    if (!pg.sort?.length) delete pg.sort;
    this.loading = true;
    try {
      this.total = yield api.count({ search });
      this.refund = yield api.get({ search, pg });
    } finally {
      this.loading = false;
    }
  }

  * create(data) {
    yield api.create(data);
    yield this.query();
  }

  * update(data) {
    yield api.update(data);
    yield this.query();
  }

  * remove(id) {
    yield api.remove(id);
    yield this.query();
  }

  * updateState(data) {
    yield api.update({ ...data, enable: !data.enable });
    yield this.query();
  }

  * getId(id) {
    const res = yield api.getId(id);
    return observable(res);
  }

  * getDriver(args) {
    return yield api.getDriver(args);
  }

  * get(args) {
    return yield api.get(args);
  }

  idx(i) {
    return this._args.pg.page * this._args.pg.size + i + 1;
  }

  get pg() {
    return this._args.pg;
  }

  set pg(pg) {
    this.query({ ...pg });
  }

  get search() {
    this._args.pg.page = 0;
    return this._args.search;
  }

  set search(search) {
    this._args.pg.page = 0;
    this._args.search = search;
    this.query({ page: 0 });
  }

  get tableSort() {
    if (this._args.pg.sort?.length) {
      const [fn, dir] = this._args.pg.sort[0].split(';');
      return { [fn]: dir };
    }
    return undefined;
  }

  /**
 * 校验是否存在交集
 * @param {*} values 输入的指
 * @param {*} state 数据源
 * @returns
 */
  uniqueState(values, state, curCharge, idx) {
    const { startPrice, endPrice } = values;
    const cloneData = _.cloneDeep(state);

    if (JSON.stringify(toJS(curCharge)) !== '{}') {
      cloneData.splice(idx, 1); // 删除当前编辑的记录
    }

    const priceStart = Number(startPrice);
    const priceEnd = Number(endPrice);

    // 公共校验：结束价格不能小于开始价格
    if (priceEnd < priceStart) {
      return '2'; // 返回错误代码，表示价格区间无效
    }

    // 检查新数据是否与现有数据有重复或交集
    for (let i = 0; i < cloneData.length; i++) {
      const rule = cloneData[i];
      const ruleStart = Number(rule.startPrice);
      const ruleEnd = Number(rule.endPrice);

      // 校验规则：检查区间是否重叠
      if (
        (priceStart >= ruleStart && priceStart <= ruleEnd) || // 新区间的开始点在现有区间内
        (priceEnd >= ruleStart && priceEnd <= ruleEnd) || // 新区间的结束点在现有区间内
        (priceStart <= ruleStart && priceEnd >= ruleEnd) // 新区间包含现有区间
      ) {
        return '1'; // 返回错误代码，表示区间重复
      }
    }

    // 如果没有发现任何问题，返回 null 或 undefined 表示校验通过
    return null;
  }
}
